﻿/**
 * 根据元数据自动生成的表单。
 */
Ext.define('Happy.form.metadata.Panel', {
    extend: 'Happy.form.Panel',
    alias: 'widget.happy-metadata-form',
    mixins: {
        'custom-configable': 'Happy.mixin.CustomConfigable',
        'form-helper': 'Happy.form.metadata.mixin.FormHelper'
    },
    requires: [
        'Happy.form.action.Save',
        'Happy.form.action.Close',
        'Happy.form.field.Checkbox',
        'Happy.form.field.CkEditor',
        'Happy.form.field.Combobox',
        'Happy.form.field.TreeCombobox',
        'Happy.form.CheckboxGroup',
        'Happy.form.ImageContainer',
        'Happy.form.LinkContainer'
    ],

    /**
     * 根据元数据配置表单 items。
     * 
     * @protected
     */
    configItems: function () {
        var me = this;

        me.items = me.autoCreateItems();
    },

    /**
     * 根据元数据配置按钮。
     * 
     * @protected
     */
    configButtons: function () {
        var me = this;

        me.buttons = me.autoCreateButtons();
    },

    /**
     * 根据元数据配置和当前表单相关的 items，如：订单的订单项列表。
     *
     * @protected
     */
    configRelatedItems: function () {
        var me = this;

        var relatedItems = me.autoCreateRelatedItems();

        if (!relatedItems) {
            return;
        }
        if (relatedItems.length == 0) {
            return;
        }

        me.addDocked({
            xtype: 'tabpanel',
            height: 200,
            dock: 'bottom',
            items: relatedItems
        }, 1);

        me.getFormPanel().on('recordloaded', function (form, record) {
            Ext.Array.each(relatedItems, function (item) {
                var grid = me.down(item.xtype);
                grid.reconfigure(record[item.plural]);
                grid.reselect();
                if (!record[item.plural].isNested) {
                    record[item.plural].reload();
                }
            });
        });
    }
});